package com.vitabuybuy.tv.ui.mine.wallet

import android.os.Bundle
import android.os.CountDownTimer
import android.text.Html
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityAddAccountBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dialog.AreaCodePopup
import com.vitabuybuy.tv.ui.WebViewActivity
import com.vitabuybuy.tv.ui.mine.wallet.viewmodel.WithdrawViewMolde
import kotlinx.android.synthetic.main.activity_add_account.*
import kotlinx.android.synthetic.main.activity_add_account.countryCodePicker
import kotlinx.android.synthetic.main.activity_add_account.etPhone
import kotlinx.android.synthetic.main.activity_find_password.*
import kotlinx.android.synthetic.main.activity_register.*

class AddAccountActivity : BaseActivity<WithdrawViewMolde, ActivityAddAccountBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_add_account
    private var defaultAreaCode = "86"


    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.bind_bank_card))
        tvReadAndAgree.text =
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                Html.fromHtml(
                    getString(R.string.read_and_agree),
                    Html.FROM_HTML_MODE_COMPACT
                )
            } else {
                Html.fromHtml(getString(R.string.read_and_agree))
            }
    }

    override fun eventHandling() {
        btnSendVerifyCode.setOnClickListener {
            when {
                etPhone.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_bank_card_phone))
                else -> viewModel.sendToPhone(
                    etPhone.text.toString(),
                    defaultAreaCode
                )
            }
        }
        btnFinish.setOnClickListener {
            when {
                etName.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_bank_card_user_name))
                etCardNumber.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_bank_number))
                tvBank.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_bank_name))
                tvsubBank.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_sub_bank_name))
                etPhone.text.isEmpty() -> XToast.normal(getString(R.string.pls_enter_bank_card_phone))
                etVerifyCode.text.isEmpty() -> XToast.normal(getString(R.string.enter_code))
                !ivChoose.isChecked -> XToast.normal(getString(R.string.pls_read_and_agree_agreement_2))
                else -> {
                    val params = HashMap<String, Any>().apply {
                        this["bankName"] = tvBank.text.toString()
                        this["holderName"] = etName.text.toString()
                        this["number"] = etCardNumber.text.toString()
                        this["subbranch"] = tvsubBank.text.toString()
                    }
                    viewModel.validateByPhone(
                        etPhone.text.toString(), etVerifyCode.text.toString(),
                        defaultAreaCode, params
                    )
                }
            }
        }
        tvReadAndAgree.setOnClickListener {
            WebViewActivity.openActivity(mContext,"","",16)
        }
        countryCodePicker.setOnClickListener{
            AreaCodePopup(this){
                countryCodePicker.text = it
                defaultAreaCode = it.replace("+","")
            }.showPopupWindow()
        }
    }

    private val timer = object : CountDownTimer((60 * 1000).toLong(), 1000) {
        override fun onTick(millisUntilFinished: Long) {
            mBinding.btnSendVerifyCode.text = String.format(
                getString(R.string.get_sms_code_s),
                millisUntilFinished / 1000
            )
            mBinding.btnSendVerifyCode.isEnabled = false
        }

        override fun onFinish() {
            mBinding.btnSendVerifyCode.text = getString(R.string.get_sms_code)
            mBinding.btnSendVerifyCode.isEnabled = true
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                XToast.success(getString(R.string.already_sms_code))
                timer.start()
            }
            2 -> {
                viewModel.addNewBankCard(msg.obj as Map<String, Any>)
            }
            3 -> {
                XToast.success(getString(R.string.add_success))
                finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, AddAccountActivity::class.java)
        }
    }
}
